package com.college.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.*;

import static org.apache.commons.lang3.exception.ExceptionUtils.getStackTrace;

/**
 * @author 小C工作室
 */
@Slf4j
public class UserUtils {

    private UserUtils() {
    }

    public static Long getHeaderUserId() {
        try {
            HttpServletRequest httpServletRequest = Objects.requireNonNull(getHttpServletRequest());
            return Long.valueOf(httpServletRequest.getHeader("shUserId"));
        } catch (Exception e) {
            e.printStackTrace();
            LogPrintUtils.info(log, "找不到用户id");
            return null;
        }
    }

    public static HttpServletRequest getHttpServletRequest() {
        try {
            return getServletRequestAttributes().getRequest();
        } catch (Exception e) {
            LogPrintUtils.info(log, "找不到当前线程中的请求");
            return null;
        }
    }

    public static ServletRequestAttributes getServletRequestAttributes() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes());
    }

}
